/*=========================================================================

  Library:   HMP

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

#include "ErrorLogAbstractMessageHandler.h"

// Qt includes
#include <QHash>
#include <QDateTime>

// HMP includes
#include "ErrorLogContext.h"

// --------------------------------------------------------------------------
// ErrorLogAbstractMessageHandlerPrivate

// --------------------------------------------------------------------------
class ErrorLogAbstractMessageHandlerPrivate
{
public:
  ErrorLogAbstractMessageHandlerPrivate();
  ~ErrorLogAbstractMessageHandlerPrivate();

  bool                        Enabled;
  QString                     HandlerPrettyName;

  // Use "int" instead of "ErrorLogModel::TerminalOutput" to avoid compilation warning ...
  // qhash.h:879: warning: passing 'ErrorLogModel::TerminalOutput' chooses 'int' over 'uint' [-Wsign-promo]
  QHash<int, ErrorLogTerminalOutput*> TerminalOutputs;
};

// --------------------------------------------------------------------------
ErrorLogAbstractMessageHandlerPrivate::
ErrorLogAbstractMessageHandlerPrivate()
  : Enabled(false)
{
}

// --------------------------------------------------------------------------
ErrorLogAbstractMessageHandlerPrivate::~ErrorLogAbstractMessageHandlerPrivate()
{
}

// --------------------------------------------------------------------------
// ErrorLogAbstractMessageHandlerPrivate methods

// --------------------------------------------------------------------------
ErrorLogAbstractMessageHandler::ErrorLogAbstractMessageHandler()
  : Superclass(), d_ptr(new ErrorLogAbstractMessageHandlerPrivate)
{
}

// --------------------------------------------------------------------------
ErrorLogAbstractMessageHandler::~ErrorLogAbstractMessageHandler()
{
}

// --------------------------------------------------------------------------
QString ErrorLogAbstractMessageHandler::handlerPrettyName()const
{
  Q_D(const ErrorLogAbstractMessageHandler);
  if (d->HandlerPrettyName.isEmpty())
    {
    return this->handlerName();
    }
  else
    {
    return d->HandlerPrettyName;
    }
}

// --------------------------------------------------------------------------
void ErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
{
  Q_D(ErrorLogAbstractMessageHandler);
  d->HandlerPrettyName = newHandlerPrettyName;
}

// --------------------------------------------------------------------------
bool ErrorLogAbstractMessageHandler::enabled()const
{
  Q_D(const ErrorLogAbstractMessageHandler);
  return d->Enabled;
}

// --------------------------------------------------------------------------
void ErrorLogAbstractMessageHandler::setEnabled(bool value)
{
  Q_D(ErrorLogAbstractMessageHandler);
  if (value == d->Enabled)
    {
    return;
    }
  this->setEnabledInternal(value);
  d->Enabled = value;
}

// --------------------------------------------------------------------------
void ErrorLogAbstractMessageHandler::handleMessage(const QString& threadId,
                                                      ErrorLogLevel::LogLevel logLevel,
                                                      const QString& origin,
                                                      const ErrorLogContext& logContext,
                                                      const QString &text)
{
  Q_D(ErrorLogAbstractMessageHandler);
  if (logLevel <= ErrorLogLevel::Info)
    {
    if(d->TerminalOutputs.contains(ErrorLogTerminalOutput::StandardOutput))
      {
      d->TerminalOutputs.value(ErrorLogTerminalOutput::StandardOutput)->output(text);
      }
    }
  else
    {
    if(d->TerminalOutputs.contains(ErrorLogTerminalOutput::StandardError))
      {
      d->TerminalOutputs.value(ErrorLogTerminalOutput::StandardError)->output(text);
      }
    }
  emit this->messageHandled(QDateTime::currentDateTime(), threadId, logLevel, origin, logContext, text);
}

// --------------------------------------------------------------------------
ErrorLogTerminalOutput* ErrorLogAbstractMessageHandler::terminalOutput(
    ErrorLogTerminalOutput::TerminalOutput terminalOutputType)const
{
  Q_D(const ErrorLogAbstractMessageHandler);
  if(d->TerminalOutputs.contains(terminalOutputType))
    {
    return d->TerminalOutputs.value(terminalOutputType);
    }
  return 0;
}

// --------------------------------------------------------------------------
void ErrorLogAbstractMessageHandler::setTerminalOutput(
    ErrorLogTerminalOutput::TerminalOutput terminalOutputType, ErrorLogTerminalOutput* terminalOutput)
{
  Q_D(ErrorLogAbstractMessageHandler);
  d->TerminalOutputs.insert(terminalOutputType, terminalOutput);
}
